package com.ruoyi.hospital.doctor.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.doctor.domain.TDoctor;
import com.ruoyi.hospital.doctor.mapper.TDoctorMapper;
import com.ruoyi.hospital.doctor.service.ITDoctorService;
import com.ruoyi.hospital.hospitalized.domain.THospitalized;
import com.ruoyi.hospital.hospitalized.mapper.THospitalizedMapper;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.mapper.TRegisteredMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 医生管理Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-20
 */
@Service
public class TDoctorServiceImpl implements ITDoctorService {
    @Autowired
    private TDoctorMapper tDoctorMapper;

    @Autowired
    private TRegisteredMapper tRegisteredMapper;

    @Autowired
    private THospitalizedMapper tHospitalizedMapper;

    /**
     * 查询医生管理
     *
     * @param id 医生管理主键
     * @return 医生管理
     */
    @Override
    public TDoctor selectTDoctorById(Long id) {
        return tDoctorMapper.selectTDoctorById(id);
    }

    /**
     * 查询医生管理列表
     *
     * @param tDoctor 医生管理
     * @return 医生管理
     */
    @Override
    public List<TDoctor> selectTDoctorList(TDoctor tDoctor) {
        return tDoctorMapper.selectTDoctorList(tDoctor);
    }

    /**
     * 新增医生管理
     *
     * @param tDoctor 医生管理
     * @return 结果
     */
    @Override
    public int insertTDoctor(TDoctor tDoctor) {
        List<TDoctor> tDoctorList = tDoctorMapper.selectTDoctorByUserId(tDoctor.getUserId());
        if (!tDoctorList.isEmpty()) {
            throw new ServiceException("医师不允许重复");
        }
        tDoctor.setCreatedTime(new Date());
        tDoctor.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tDoctorMapper.insertTDoctor(tDoctor);
    }

    /**
     * 修改医生管理
     *
     * @param tDoctor 医生管理
     * @return 结果
     */
    @Override
    public int updateTDoctor(TDoctor tDoctor) {
        return tDoctorMapper.updateTDoctor(tDoctor);
    }

    /**
     * 批量删除医生管理
     *
     * @param ids 需要删除的医生管理主键
     * @return 结果
     */
    @Override
    public int deleteTDoctorByIds(String ids) {
        //删除校验
        String[] array = Convert.toStrArray(ids);
        for (String id : array) {
            checkDelete(id);
        }
        return tDoctorMapper.deleteTDoctorByIds(Convert.toStrArray(ids));
    }

    private void checkDelete(String doctorid) {
        TRegistered tRegistered = new TRegistered();
        tRegistered.setDoctorId(doctorid);
        List<TRegistered> tRegisteredList = tRegisteredMapper.selectTRegisteredList(tRegistered);
        if (!tRegisteredList.isEmpty()) {
            throw new ServiceException("已有门诊引用，不能删除医生");
        }
        THospitalized tHospitalized = new THospitalized();
        tHospitalized.setDoctorId(doctorid);
        List<THospitalized> tHospitalizedList = tHospitalizedMapper.selectTHospitalizedList(tHospitalized);
        if (!tHospitalizedList.isEmpty()) {
            throw new ServiceException("已有住院管理引用，不能删除医生");
        }
    }

    /**
     * 删除医生管理信息
     *
     * @param id 医生管理主键
     * @return 结果
     */
    @Override
    public int deleteTDoctorById(Long id) {
        return tDoctorMapper.deleteTDoctorById(id);
    }
}
